// pages/view/view.js
//获取应用实例
const app = getApp();
//在使用的View中引入WxParse模块
var WxParse = require('../../wxParse/wxParse.js');
var scrollTop = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    content: [],
    openid: '',
    chapter: '',
    animationData: {},
    bottomShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中...',
    })
    wx.showNavigationBarLoading()
    this.setData({
      'openid': app.globalData.openid,
      'chapter': options.chapter
    });
    if (!this.data.openid) {
      // 调用云函数
      wx.cloud.callFunction({
        name: 'login',
        data: {},
        success: res => {
          console.log('[云函数] [login] user openid: ', res.result.openid)
          app.globalData.openid = res.result.openid
          this.setData({
            openid: res.result.openid
          })
        },
        fail: err => {
          console.error('[云函数] [login] 调用失败', err)
        }
      })
    }
    this.fetchData(options.chapter);
    if (options.scrollTop > 0) {
      scrollTop = options.scrollTop;
    }
    this.setAnimation();
  },

  //给a标签添加跳转
  wxParseTagATap: function(e) {
    var self = this;
    var href = e.currentTarget.dataset.src;
    var url = '/pages/view/view?chapter=' + href;
    scrollTop = 0;
    wx.request({
      url: 'https://xs.ooooooooooo.cn/history.php?mod=add_history&openid=' + this.data.openid + '&chapter=' + href + '&scrollTop=0',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res.data)
      }
    })
    wx.redirectTo({
      url: url
    })
  },

  //获取列表内容
  fetchData: function(chapter) {
    var self = this;
    wx.request({
      url: "https://xs.ooooooooooo.cn/xcx.php?mod=view&chapter=" + chapter,
      success: function(res) {
        console.log('拉取列表成功', res)
        var title = res.data.title
        self.setData({
          title: title
        })
        WxParse.wxParse('content', 'html', res.data.content, self, 5)
        WxParse.wxParse('next', 'html', res.data.next, self, 5)
        setTimeout(function() {
          wx.hideLoading()
          wx.hideNavigationBarLoading()
          if (scrollTop > 0) {
            wx.pageScrollTo({
              scrollTop: scrollTop,
              duration: 500
            })
          }
        }, 1000)
        wx.setNavigationBarTitle({
          title: res.data.title,
          success: function(res) {
            // success
          }
        });
      },
      fail: function(res) {
        console.log('拉取列表失败', res)
      }
    })
  },

  shuqianTap: function() {
    wx.request({
      url: 'https://xs.ooooooooooo.cn/history.php?mod=add_history&openid=' + this.data.openid + '&chapter=' + this.data.chapter + '&scrollTop=' + scrollTop,
      success: function(res) {
        console.log(res.data)
        if (res.data.errcode == 0) {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  },

  indexTap: function() {
    var url = '/pages/index/index';
    wx.redirectTo({
      url: url
    })
  },

  setAnimation: function() {
    var animation = wx.createAnimation({
      duration: 0,
      timingFunction: 'ease',
    })
    this.animation = animation
  },

  bottomTap: function(event) {
    if (this.data.bottomShow === false) {
      this.animation.translateY(-60).step({
        duration: 1000
      })
      this.setData({
        animationData: this.animation.export(),
        bottomShow: true
      })
    } else {
      this.animation.translateY(60).step({
        duration: 1000
      })
      this.setData({
        animationData: this.animation.export(),
        bottomShow: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.shuqianTap();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.bottomShow === false) {
      this.animation.translateY(-60).step({
        duration: 1000
      })
      this.setData({
        animationData: this.animation.export(),
        bottomShow: true
      })
    }
  },

  onPageScroll: function(res) {
    // Do something when page scroll
    /*this.setData({
        scrollTop: res.scrollTop
      })*/
    scrollTop = res.scrollTop
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})